package models

import (
	"fmt"
	"xorm.io/xorm"
	"zorm/consts"
)

type Migrator struct {
	DBType string
	RealDB interface{}
}

func (m *Migrator) CreateTable(values ...interface{}) error {
	var (
		err error
		value interface{}
		noExistValues = make([]interface{},0)
		exist bool
	)
	if m.DBType == consts.SqlDatabaseStr{
		for _,value = range values{
			if exist,_ = m.RealDB.(*xorm.Engine).IsTableExist(value);!exist{
				noExistValues = append(noExistValues,value)
			}
		}
		err = m.RealDB.(*xorm.Engine).CreateTables(noExistValues...)
		//err = m.RealDB.(*xorm.Engine).Sync2(values...)
	}else{

	}
	if err != nil{
		fmt.Println(err)
		return err
	}
	return nil
}

func (m *Migrator) DropTable(values ...interface{}) error {
	var (
		err error
	)
	if m.DBType == consts.SqlDatabaseStr{
		err = m.RealDB.(*xorm.Engine).DropTables(values...)
		//err = m.RealDB.(*xorm.Engine).Sync2(values...)
	}else{

	}
	if err != nil{
		fmt.Println(err)
		return err
	}
	return nil
}

func (m *Migrator) AutoMigrate(values ...interface{}) error {
	var (
		err error
	)
	if m.DBType == consts.SqlDatabaseStr{
		err = m.RealDB.(*xorm.Engine).Sync2(values...)
		//err = m.RealDB.(*xorm.Engine).Sync2(values...)
	}else{

	}
	if err != nil{
		fmt.Println(err)
		return err
	}
	return nil
}

